package com.boc.ewr.outsrc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.boc.ewr.bcimp.entity.CaSendDtl;
import com.boc.ewr.bcimp.service.ICaSendDtlService;
import com.boc.ewr.outsrc.entity.OutsrcExpStlmtBulk;
import com.boc.ewr.outsrc.entity.OutsrcStateAnalysis;
import com.boc.ewr.outsrc.mapper.OutsrcStateAnalysisMapper;
import com.boc.ewr.outsrc.service.IOutsrcDailyProblemService;
import com.boc.ewr.outsrc.service.IOutsrcExpStlmtBulkService;
import com.boc.ewr.outsrc.service.IOutsrcSptckProblemService;
import com.boc.ewr.outsrc.service.IOutsrcStateAnalysisService;
import com.boc.ewr.util.file.enumeration.BillStatusEnum;
import com.sun.mail.util.LineOutputStream;
import org.apache.ibatis.annotations.Case;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.vo.DictModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 外包公司情况分析表
 * @Author: jeecg-boot
 * @Date: 2023-06-18
 * @Version: V1.0
 */
@Service
public class OutsrcStateAnalysisServiceImpl extends ServiceImpl<OutsrcStateAnalysisMapper, OutsrcStateAnalysis> implements IOutsrcStateAnalysisService {

    @Autowired
    IOutsrcDailyProblemService outsrcDailyProblemService;

    @Autowired
    IOutsrcSptckProblemService outsrcSptckProblemService;

    @Autowired
    private ISysBaseAPI sysBaseAPI;

    @Autowired
    private IOutsrcExpStlmtBulkService outsrcExpStlmtBulkService;

    @Autowired
    private ICaSendDtlService caSendDtlService;

    @Autowired
    private OutsrcStateAnalysisMapper outsrcStateAnalysisMapper;

    @Override
    public void generateInfo(Date start, Date end) {
        //清理之前的数据，每半年生成一次
        outsrcStateAnalysisMapper.delete(null);

        //日常问题数
        List<OutsrcStateAnalysis> dailyProblemList = outsrcStateAnalysisMapper.genDailyProblem(start, end);

        //现场检查
        List<OutsrcStateAnalysis> sptckProblemList = outsrcStateAnalysisMapper.genSptckProblem(start, end);

        //违约份数
        /*外包逾期回收
        未回收
        外包遗失
        虚假退信
        客户投诉*/
        List<OutsrcStateAnalysis> defaultedShareList = outsrcStateAnalysisMapper.genDefaultedShare(start, end);
        for (OutsrcStateAnalysis outsrcStateAnalysis : defaultedShareList) {
            String branchNo = outsrcStateAnalysis.getBranchNo();
            DictModel parentDepartId = sysBaseAPI.getParentDepartId(branchNo);
            String text = parentDepartId.getText();
            outsrcStateAnalysis.setBranchNo(text);
            // 在散投里查询违约，并加上
            long bulkCount = outsrcExpStlmtBulkService.count(
                    new LambdaQueryWrapper<OutsrcExpStlmtBulk>()
                            .eq(OutsrcExpStlmtBulk::getBranchNo, outsrcStateAnalysis.getBranchNo())
                            .in(OutsrcExpStlmtBulk::getBillStatus, BillStatusEnum.getDefaultIndexSet())
                            .between(OutsrcExpStlmtBulk::getBillMon, start, end)
            );
            if (bulkCount > 0) {
                long total = bulkCount + Long.getLong(outsrcStateAnalysis.getDefaultNum());
                outsrcStateAnalysis.setDefaultNum(String.valueOf(total));
            }
            // 查询总数计算百分比
            long allCount = caSendDtlService.count(
                    new QueryWrapper<CaSendDtl>()
                            .eq("sign_branch_no", outsrcStateAnalysis.getBranchNo())
                            .between("TO_DATE(CA_DATE, 'yyyyMMdd')", start, end)
            );
            if (allCount > 0) {
                double result = (double) Long.getLong(outsrcStateAnalysis.getDefaultNum()) / allCount * 100;
                outsrcStateAnalysis.setDefaultRatio(String.format("%.2f%%", result));
            }
        }

        // 导入数据库
        List<OutsrcStateAnalysis> list = new ArrayList<>(dailyProblemList);
        list.addAll(sptckProblemList);
        list.addAll(defaultedShareList);

        this.saveBatch(list);
    }
}
